//
//  CLCustomContentView.m
//  Charles
//
//  Created by Charles on 2019/7/8.
//  Copyright © 2019 charles. All rights reserved.
//

#import "CLCustomContentView.h"

@interface CLCustomContentView ()

@property (nonatomic, strong) UIButton *outboundsButton;

@end

@implementation CLCustomContentView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        [self setupSubViews];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UIResponder *next = self.nextResponder;
    NSLog(@"%@ Touched!! next is %@",NSStringFromClass(self.class),NSStringFromClass(next.class));
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"*****************%@",NSStringFromClass(self.class));

    if (self.alpha < 0.01 || !self.userInteractionEnabled || self.isHidden) {
        return nil;
    }
    if ([self pointInside:point withEvent:event]) {
        __block UIView *hit = nil;
        [self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGPoint convertPoint = [self convertPoint:point toView:obj];
            hit = [obj hitTest:convertPoint withEvent:event];
            NSLog(@"----%@",NSStringFromClass(obj.class));
            if (hit) {
                *stop = YES;
            }
        }];
        if (hit) {
            return hit;
        }else {
            return self;
        }
    }else {
        // 点击超出Button父视图的Button的可视范围
        // 判断当前点击的点是否在Button范围内,如果在的话，返回响应的视图是self.outboundsButton
        CGPoint convertPoint = [self.outboundsButton convertPoint:point fromView:self];
        if ([self.outboundsButton pointInside:convertPoint withEvent:event]) {
            return self.outboundsButton;
        }
    }
    return nil;
}


- (void)setupSubViews {
    [self addSubview:self.titleLabel];
    self.titleLabel.text = @"CLCustomContentView";
    
//    self.outboundsButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.outboundsButton.backgroundColor = [UIColor redColor];
//    self.outboundsButton.frame = CGRectMake(0, self.bounds.size.height - 20, 80, 50);
//    self.outboundsButton.backgroundColor = [UIColor yellowColor];
//    [self.outboundsButton addTarget:self action:@selector(outboundsEvent:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:self.outboundsButton];
}

- (void)outboundsEvent:(UIButton *)sender {
    NSLog(@"点击可以响应");
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 20)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

@implementation CLRedView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"*****************%@",NSStringFromClass(self.class));
    if (self.alpha < 0.01 || !self.userInteractionEnabled || self.isHidden) {
        return nil;
    }
    if ([self pointInside:point withEvent:event]) {
        __block UIView *hit = nil;
        [self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGPoint convertPoint = [self convertPoint:point toView:obj];
            hit = [obj hitTest:convertPoint withEvent:event];
            NSLog(@"----%@",NSStringFromClass(obj.class));
            if (hit) {
                *stop = YES;
            }
        }];
        if (hit) {
            return hit;
        }else {
            return self;
        }
    }
    return nil;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UIResponder *next = self.nextResponder;
    NSLog(@"%@ Touched!! next is %@",NSStringFromClass(self.class),NSStringFromClass(next.class));
}

@end

@implementation CLYellowView

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UIResponder *next = self.nextResponder;
    NSLog(@"%@ Touched!! next is %@",NSStringFromClass(self.class),NSStringFromClass(next.class));
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"*****************%@",NSStringFromClass(self.class));
    
    if (self.alpha < 0.01 || !self.userInteractionEnabled || self.isHidden) {
        return nil;
    }
    if ([self pointInside:point withEvent:event]) {
        __block UIView *hit = nil;
        [self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGPoint convertPoint = [self convertPoint:point toView:obj];
            hit = [obj hitTest:convertPoint withEvent:event];
            NSLog(@"----%@",NSStringFromClass(obj.class));
            if (hit) {
                *stop = YES;
            }
        }];
        if (hit) {
            return hit;
        }else {
            return self;
        }
    }
    return nil;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    CGFloat x1 = point.x;
    CGFloat y1 = point.y;
    
    CGFloat x2 = self.frame.size.width / 2;
    CGFloat y2 = self.frame.size.height / 2;
    
    double dis = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    if (dis <= self.frame.size.width / 2) {
        return YES;
    }else {
        return NO;
    }
}

- (void)helloword {
    NSLog(@"hello word!!");
}

@end

@implementation CLBlueView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"*****************%@",NSStringFromClass(self.class));
    if (self.alpha < 0.01 || !self.userInteractionEnabled || self.isHidden) {
        return nil;
    }
    if ([self pointInside:point withEvent:event]) {
        __block UIView *hit = nil;
        [self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGPoint convertPoint = [self convertPoint:point toView:obj];
            hit = [obj hitTest:convertPoint withEvent:event];
            NSLog(@"----%@",NSStringFromClass(obj.class));
            if (hit) {
                *stop = YES;
            }
        }];
        if (hit) {
            return hit;
        }else {
            return self;
        }
    }
    return nil;
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    UIResponder *next = self.nextResponder;
//    NSLog(@"%@ Touched!! next is %@",NSStringFromClass(self.class),NSStringFromClass(next.class));
//}

@end
